from django.db import models


# Create your models here.
class ParkingLot(models.Model):
    """
        场库基本信息，需要POST给车管所签到,请求：
        {“seqno”:1,“code”:“P011”,”
        commRequest”: {“clientId”:”001”,”tsn”:”1234567890”,”sim”:”1234567890”,”pasm”:”1234567890”,”sysVer”:”wince5”,”appVer”:”pm1.0”},
        ”uid”:”010001”,”pwd”:”123456”,”longi”:”123.4567”,”lati”:”23.4567”,”batchcode”:”2011080101000101”,”name”:”华宁国际停车场”,”address”:”宣化路300号”,”opentime”:”24小时”,”price”:”10元每小时”}
        响应：
        {“seqno”:1,“code”:“P011”, “commResponse”:{“result”:”1”,”systime”:”20110701104559” }}

    """
    clientId = models.CharField(u'客户编号', unique=True, max_length=50, null=True,
                                blank=True)  # 车收费系统厂商维护的停车场客户编号，不超过20个字符
    tsn = models.CharField(u'终端序列号', unique=True, max_length=50, null=True, blank=True)  # 数据采集器的硬件编号，不超过16个字符
    sim = models.CharField(u'SIM卡号', unique=True, max_length=50, null=True, blank=True)  # 可以是手机号码，ICCID号，空卡序列号，不超过20字符
    psam = models.CharField(u'PSAM卡号', unique=True, max_length=50, null=True, blank=True)  # 交通卡公司提供的，不超过12字符
    sysVer = models.CharField(u'系统版本号', unique=True, max_length=50, null=True,
                              blank=True)  # 指数据采集器的系统版本，如wince5.0或android4.3等，不超过20字符
    appVer = models.CharField(u'应用版本号', unique=True, max_length=50, null=True,
                              blank=True)  # 数据采集器的软件应用版本，要求新版本号与旧版本号进行字符串比较的结果是大于，不超过20字符

    parkingSpotId = models.CharField(u'停车点编号', unique=True, max_length=50, null=True, blank=True)       #交管局给的
    platformId = models.CharField(u'平台编号', unique=True, max_length=50, null=True, blank=True)           #交管局给的
    longi = models.CharField(u'经度', unique=True, max_length=50, null=True, blank=True)
    lati = models.CharField(u'纬度', unique=True, max_length=50, null=True, blank=True)
    parking_name = models.CharField(u'停车库名称', unique=True, max_length=100, null=True, blank=True)
    parking_add = models.CharField(u'停车库地址', unique=True, max_length=200, null=True, blank=True)
    price = models.CharField(u'收费标准', unique=True, max_length=200, null=True, blank=True)       #停车场收费标准描述
    pwd = models.CharField(u' 场库密码', unique=True, max_length=200, null=True, blank=True)       #停车场收费标准描述

    totBerthNum = models.PositiveSmallIntegerField(u'总车位数', default=1, blank=True)
    monthlyBerthNum = models.PositiveSmallIntegerField(u'月租车位', default=1, blank=True)
    guesBerthNum = models.PositiveSmallIntegerField(u'访客车位', default=1, blank=True)
    work_time_st = models.TimeField(u'工作开始时间', null=True)
    work_time_ed = models.TimeField(u'工作结束时间', null=True)

    def __str__(self):
        return 'ID%s --  %s  --  %s' % (self.id, self.parking_name, self.totBerthNum)

    class Meta:
        verbose_name = '停车场基本信息'
        verbose_name_plural = '停车场基本信息表'


class ClientList(models.Model):
    """
    记录客户端的IP地址，也作为出入口数量、安装信息的记录
    """
    uid = models.CharField(u'出入口UID', unique=True, max_length=20, null=True, blank=True)      #提供给平台,不知道这是出入口的编号还是操作员的编号
    position_name = models.CharField(u'出入口名称', unique=True, max_length=50, null=True, blank=True)
    # position_num = models.CharField(u'出入口编号', unique=True, max_length=50, null=True, blank=True)
    prc_ip = models.GenericIPAddressField(u'客户端IP', unique=True, null=True, blank=True)  # IP地址唯一
    way_num = models.SmallIntegerField(u'通道号', default=1, blank=True)
    install_position = models.CharField(u'安装位置', max_length=50, null=True, blank=True)

    def __str__(self):
        return '%s  --  %s' % (self.install_position, self.prc_ip)

    class Meta:
        verbose_name = '出入口客户端'
        verbose_name_plural = '出入口客户端配置表'


class CameraPosition(models.Model):
    """
    出入口摄像头，继电器安装的
    """

    position = models.ForeignKey('manager.ClientList', verbose_name='出入口信息', null=True, blank=True)
    camera_ip = models.GenericIPAddressField(u'摄像头IP', unique=True, null=True, blank=True)  # IP地址唯一,这个目前不需要，都由客户端snap自己搞定

    realy_ip = models.GenericIPAddressField(u'抬杆继电器IP', unique=True, null=True, blank=True)  # IP地址唯一
    realy_num = models.PositiveSmallIntegerField(u'抬杆继电器管道号', null=True, blank=True)

    def __str__(self):
        return '%s  --  %s  --  %s' % (self.position.position_name, self.camera_ip, self.realy_ip)

    class Meta:
        verbose_name = '摄像头'
        verbose_name_plural = '摄像头配置表'


class Expense(models.Model):
    """
    计费公式
    """
    free_time = models.PositiveSmallIntegerField(u'免费时长（）', default=0.0, null=True, blank=True)

    basic = models.PositiveSmallIntegerField(u'起步价', default=0.0, null=True, blank=True)
    basic_time = models.PositiveSmallIntegerField(u'起步时长', default=0.0, null=True, blank=True)

    period_one = models.PositiveSmallIntegerField(u'第一阶梯价', default=0.0, null=True, blank=True)
    period_one_time = models.PositiveSmallIntegerField(u'第一阶梯时长', default=0.0, null=True, blank=True)

    period_two = models.PositiveSmallIntegerField(u'第二阶梯价', default=0.0, null=True, blank=True)
    period_two_time = models.PositiveSmallIntegerField(u'第二阶梯时长', default=0.0, null=True, blank=True)

    max_day = models.PositiveSmallIntegerField(u'当日最高价', default=0.0, null=True, blank=True)

    leisure = models.PositiveSmallIntegerField(u'闲时价格', default=0.0, null=True, blank=True)
    leisure_st = models.DateTimeField(u'闲时开始时间', null=True)
    leisure_en = models.DateTimeField(u'闲时结束时间', null=True)
    is_active = models.BooleanField(u'计费模式有效', default=1)

    def __str__(self):
        return '基础价：%s元,   每日最高收费：%s元' % (self.basic, self.max_day)

    class Meta:
        verbose_name = '计费'
        verbose_name_plural = '计费方式表'

#
# class ShppisLog(models.Model):  # 所有发送到平台的数据
#     seqno = models.IntegerField(u'流水号', null=True, blank=True)
#     # message = models.BinaryField(u'发送到平台的内容', null=True, blank=True)
#     message = models.CharField(u'发送到平台的内容', max_length=1000, null=True, blank=True)
#     acttime = models.DateTimeField(u'发送时间', null=True)
#     receive = models.BooleanField(u'接收平台回复', default=0, blank=True)
#     def __str__(self):
#         return '%s  --  %s' % (self.acttime, self.receive)
#
#     class Meta:
#         verbose_name = '平台通讯详情'
#         verbose_name_plural = '平台通讯详情表'

class VirtualParking(models.Model):

    parkingnum = models.CharField(u'泊位编号', max_length=20, null=True, blank=True)
    parkingproof = models.CharField(u'停车凭证号', max_length=50, null=True, blank=True)
    CAR_TYPE = ((1, '月租车位'), (2, '访客车位'))
    parkingtype = models.PositiveSmallIntegerField(u'泊位类型', choices=CAR_TYPE, default=1, null=True, blank=True)        #  1 月租车位，2 访客车位， 3临时车位
    apply = models.BooleanField(u'车位使用', default=0, blank=True)
    def __str__(self):
        return '%s  --  %s' % (self.parkingnum, self.apply)
    class Meta:
        verbose_name = '泊位详情'
        verbose_name_plural = '泊位详情表'

class EventLog(models.Model):
    """
    车辆流水及收费明细,展示用
    """
    card_num = models.CharField(u'车牌号', max_length=50, null=True, blank=True)
    virtualParking = models.ForeignKey('manager.VirtualParking', verbose_name='virtualParking', null=True, blank=True)
    CAR_TYPE = ((1, '月租'), (3, '临时车'), (2, '特殊车辆'))
    car_type = models.PositiveSmallIntegerField(u'车辆类型', choices=CAR_TYPE, default=3, null=True)

    in_pic = models.ImageField(upload_to='./upload', null=True, blank=True)
    in_timer = models.DateTimeField(u'进库时间', null=True)
    in_actType = models.PositiveSmallIntegerField(u'进库类型', default=102, null=True)
    in_batchCode = models.CharField(u'到达批次号', max_length=50, null=True,blank=True)
    in_bizSn = models.PositiveIntegerField(u'到达业务流水号', default=1, null=True)

    out_pic = models.ImageField(upload_to='./upload', null=True, blank=True)
    out_timer = models.DateTimeField(u'离开时间', null=True)
    out_actType = models.PositiveSmallIntegerField(u'离开类型', default=212, null=True)
    out_batchCode = models.CharField(u'离开批次号', max_length=50, null=True,blank=True)
    out_bizSn = models.PositiveIntegerField(u'离开业务流水号', default=1, null=True)

    counteract = models.BooleanField(u'入出抵消', default=0, blank=True)  # 为0则只进入了，为1则有进出记录

    # 0：自动识别，1 ：手动识别
    avtomatik = models.PositiveSmallIntegerField(u'自动识别', default=0, blank=True)
    factMoney = models.FloatField(u'计费金额', default=0.00, null=True)
    factDiscount = models.FloatField(u'计费折扣', default=0.00, null=True)
    dueMoney = models.FloatField(u'应缴金额', default=0.00, null=True)
    payMoney = models.FloatField(u'收费金额/实缴金额', default=0.00, null=True)
    paymentType = models.PositiveSmallIntegerField(u'支付类型', default=2, null=True)  # 包月则不填，1-交通卡刷卡；（道路）2-现金；（道路）3-管卡；（道路） 10代表现金支付，（场库）11代表交通卡支付，（场库）12代表银行卡支付，（场库）13代表手机支付，

    payDiscount = models.FloatField(u'实缴折扣', default=0.00, null=True)
    dueBalance = models.FloatField(u'剩余应付', default=0.00, null=True)
    prepayTimeLength = models.PositiveSmallIntegerField(u'预付费时长', default=0, null=True)
    prepayMoney = models.FloatField(u'预付费金额', default=0.00, null=True)
    prepayDiscount = models.FloatField(u'预付费折扣', default=0.00, null=True)
    compensateMoney = models.FloatField(u'补缴金额', default=0.00, null=True)
    # wuxm add 增加记录入库和更新时间
    create_at = models.DateTimeField(blank=True, auto_now_add=True, null=True)
    update_at = models.DateTimeField(blank=True, auto_now=True, null=True)

    def __str__(self):
        return '%s  --  %s  --  %s  --  %s' % (self.card_num, self.in_timer,
                                               self.out_timer, self.payMoney)

    class Meta:
        verbose_name = '收费明细'
        verbose_name_plural = '收费明细表'

class VipEventLog(models.Model):
    """
    车辆流水及收费明细,展示用
    """
    card_num = models.CharField(u'车牌号', max_length=50, null=True, blank=True)
    virtualParking = models.ForeignKey('manager.VirtualParking', verbose_name='virtualParking', null=True, blank=True)
    CAR_TYPE = ((1, 'VIP'), (3, '临时车'), (2, '特殊车辆'))
    car_type = models.PositiveSmallIntegerField(u'车辆类型', choices=CAR_TYPE, default=1, null=True)

    in_pic = models.ImageField(upload_to='./upload', null=True, blank=True)
    in_timer = models.DateTimeField(u'进库时间', null=True)

    out_pic = models.ImageField(upload_to='./upload', null=True, blank=True)
    out_timer = models.DateTimeField(u'离开时间', null=True)

    counteract = models.BooleanField(u'入出抵消', default=0, blank=True)  # 为0则只进入了，为1则有进出记录

    # 0：自动识别，1 ：手动识别
    avtomatik = models.BooleanField(u'自动识别', default=True, blank=True)

    # wuxm add 增加记录入库和更新时间
    create_at = models.DateTimeField(blank=True, auto_now_add=True, null=True)
    update_at = models.DateTimeField(blank=True, auto_now=True, null=True)

    def __str__(self):
        return '%s  -- %s  -- %s  --  %s' % (self.card_num, self.car_type, self.in_timer, self.out_timer)

    class Meta:
        verbose_name = 'VIP车库进出明细'
        verbose_name_plural = 'VIP车库进出明细表'
